    program robinhood_deciles
    character*5 ticker(5000)
    character*8 cusp(5000)
    character*9 temp9
    character*24 raw
    integer(kind=8) nshrs(5000),nvalue(5000),jvalue(5000),ivol(5000),numshrs(5000)
    dimension jast(5000),jdate(5000),itickt(5000),age(5000),favg(5000),fmax(5000),fmin(5000),fstd(5000),iperm(5000),numhld(5000)
    dimension  price(5000),ishrout(5000),vwret(5000),util(5000),retrn(5000),dfee(5000),nep(5000),ibgtck(200),turn(5000)
    dimension dprice(10),dechld(10),decturn(10),totprc(10),totrobin(10),totturn(10)
!      open(unit=10,file='e:\FIS_duration\episode_data_with_robinhood.txt')
!      open(unit=99,file='e:\FIS_duration\robinhood_turn_price_deciles.txt')
      open(unit=10,file='e:\FIS_duration\episode_data_with_robinhood_rep.txt')
      open(unit=99,file='e:\FIS_duration\robinhood_turn_price_deciles_rep.txt')
      iend=0
      i=0
1     i=i+1      
2     read(10,1000,end=900)nep(i),jast(i),jdate(i),cusp(i),ticker(i),itickt(i),numshrs(i),age(i),favg(i),iperm(i),price(i),ivol(i),retrn(i),ishrout(i),vwret(i),util(i),nshrs(i),numhld(i)     
1000  format(1x,i4,2(1x,i8),1x,a8,2x,a5,5x,i9,1x,i14,2(1x,f11.4),1x,i5,1x,f9.2,1x,i10,1x,f10.6,1x,i9,1x,f9.6,1x,f6.2,1x,i13,1x,i7) 
      if(numhld(i).lt.0) go to 2
      turn(i)=float(ivol(i))/(1000.*float(ishrout(i)))
      if(nep(i).eq.nep(1)) go to 1
5     icnt=i-1     
      if(icnt.lt.30) go to 500
      arobin=0.
      aprice=0.
      do 20 ii=1,icnt
          arobin=arobin+float(numhld(ii))/float(icnt)
20    aprice=aprice+price(ii)/float(icnt)
!      write(6,6000)nep(1),arobin,aprice
!6000  format(1x,i4,1x,f8.0,1x,f7.2)   
      icntm=icnt-1
      do 40 i1=1,icntm
          ip=i1+1
          do 30 i2=ip,icnt
              if(turn(i1).lt.turn(i2)) go to 30
              tmp1=turn(i1)
              tmp2=price(i1)
              itmp1=numhld(i1)
              turn(i1)=turn(i2)
              price(i1)=price(i2)
              numhld(i1)=numhld(i2)
              turn(i2)=tmp1
              price(i2)=tmp2
              numhld(i2)=itmp1
30        continue
40    continue
      rdec=(float(icnt)+.01)/10.
      idec=int(rdec)
      idif=icnt-10*idec
      dprice=0
      dechld=0.
      decturn=0.
      do 60 n=1,4
          nbg=(n-1)*idec+1
          nend=n*idec
          do 50 nn=nbg,nend
          dprice(n)=dprice(n)+price(nn)/float(idec)
          dechld(n)=dechld(n)+float(numhld(nn))/float(idec)
          decturn(n)=decturn(n)+turn(nn)/float(idec)
50        continue
60    continue
      nbg=4*idec+1
      nend=5*idec+idif
          do 70 nn=nbg,nend
          dprice(5)=dprice(5)+price(nn)/(float(idec)+float(idif))
          dechld(5)=dechld(5)+float(numhld(nn))/(float(idec)+float(idif))
          decturn(5)=decturn(5)+turn(nn)/(float(idec)+float(idif))
70        continue
      do 90 n=6,10
          nbg=(n-1)*idec+1+idif
          nend=n*idec+idif
          do 80 nn=nbg,nend
          dprice(n)=dprice(n)+price(nn)/float(idec)
          dechld(n)=dechld(n)+float(numhld(nn))/float(idec)
          decturn(n)=decturn(n)+turn(nn)/float(idec)
80        continue
90    continue
      do 95 n=1,10
          dprice(n)=dprice(n)/aprice
          dechld(n)=dechld(n)/arobin
          write(6,6001)nep(1),n,decturn(n),dprice(n),dechld(n)
          write(99,6001)nep(1),n,decturn(n),dprice(n),dechld(n)
6001      format(1x,i4,1x,i2,1x,f9.5,1x,f7.4,1x,f8.5)   
95    continue          
      
500   jast(1)=jast(i)
      jdate(1)=jdate(i)
      cusp(1)=cusp(i)
      ticker(1)=ticker(i)
      itickt(1)=itickt(i)
      numshrs(1)=numshrs(i)
      jvalue(1)=jvalue(i)
      age(1)=age(i)
      favg(1)=favg(i)
      iperm(1)=iperm(i)
      price(1)=price(i)
      ivol(1)=ivol(i)
      retrn(1)=retrn(i)
      ishrout(1)=ishrout(i)
      vwret(1)=vwret(i)
      util(1)=util(i)
      nshrs(1)=nshrs(i)
      nvalue(1)=nvalue(i) 
      dfee(1)=dfee(i)
      nep(1)=nep(i)
      numhld(1)=numhld(i)
      turn(1)=turn(i)
      i=1
      go to 1
900   iend=iend+1
      if(iend.eq.1) go to 20 
      stop

    end program robinhood_deciles
